package com.quiz.source;

import java.util.Vector;

import com.quiz.source.database.QuizDatabaseHelper;
import com.quiz.source.database.QuizDbAdapter;
import com.quiz.source.model.details;


import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

@SuppressWarnings("unused")
public class mySports extends Activity implements OnCheckedChangeListener{
	
	private QuizDbAdapter dbHelper;
	private Button next;
	private RadioGroup quest1_group;
	private TextView q1_mquestion;
	private RadioButton q1_moption1;
	private RadioButton q1_moption2;
	private RadioButton q1_moption3;
	private RadioButton q1_moption4;
	private RadioGroup quest2_group;
	private TextView q2_mquestion;
	private RadioButton q2_moption1;
	private RadioButton q2_moption2;
	private RadioButton q2_moption3;
	private RadioButton q2_moption4;
	private RadioGroup quest3_group;
	private TextView q3_mquestion;
	private RadioButton q3_moption1;
	private RadioButton q3_moption2;
	private RadioButton q3_moption3;
	private RadioButton q3_moption4;
	private RadioGroup quest4_group;
	private TextView q4_mquestion;
	private RadioButton q4_moption1;
	private RadioButton q4_moption2;
	private RadioButton q4_moption3;
	private RadioButton q4_moption4;
	private RadioGroup quest5_group;
	private TextView q5_mquestion;
	private RadioButton q5_moption1;
	private RadioButton q5_moption2;
	private RadioButton q5_moption3;
	private RadioButton q5_moption4;
	//private TextView manswer;
	private Long mRowId;
	private String mCategory="sports";
	private details mTodo = null;
	private int count = 0;
	Vector<String> v1 = new Vector<String>();
	Vector<String> v2 = new Vector<String>();
	Vector<String> v3 = new Vector<String>();
	Vector<String> v4 = new Vector<String>();
	Vector<String> v5 = new Vector<String>();
	Cursor cursor;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        setContentView(R.layout.questions);
        next = (Button) findViewById(R.id.submit);
        quest1_group = (RadioGroup) findViewById(R.id.q1_options);
        quest1_group.setOnCheckedChangeListener(this);
        q1_mquestion = (TextView) findViewById(R.id.question1_text);
        q1_moption1 = (RadioButton) findViewById(R.id.q1_option1);
        q1_moption2 = (RadioButton) findViewById(R.id.q1_option2);
        q1_moption3 = (RadioButton) findViewById(R.id.q1_option3);
        q1_moption4 = (RadioButton) findViewById(R.id.q1_option4);
        quest2_group = (RadioGroup) findViewById(R.id.q2_options);
        quest2_group.setOnCheckedChangeListener(this);
        q2_mquestion = (TextView) findViewById(R.id.question2_text);
        q2_moption1 = (RadioButton) findViewById(R.id.q2_option1);
        q2_moption2 = (RadioButton) findViewById(R.id.q2_option2);
        q2_moption3 = (RadioButton) findViewById(R.id.q2_option3);
        q2_moption4 = (RadioButton) findViewById(R.id.q2_option4);
        quest3_group = (RadioGroup) findViewById(R.id.q3_options);
        quest3_group.setOnCheckedChangeListener(this);
        q3_mquestion = (TextView) findViewById(R.id.question3_text);
        q3_moption1 = (RadioButton) findViewById(R.id.q3_option1);
        q3_moption2 = (RadioButton) findViewById(R.id.q3_option2);
        q3_moption3 = (RadioButton) findViewById(R.id.q3_option3);
        q3_moption4 = (RadioButton) findViewById(R.id.q3_option4);
        quest4_group = (RadioGroup) findViewById(R.id.q4_options);
        quest4_group.setOnCheckedChangeListener(this);
        q4_mquestion = (TextView) findViewById(R.id.question4_text);
        q4_moption1 = (RadioButton) findViewById(R.id.q4_option1);
        q4_moption2 = (RadioButton) findViewById(R.id.q4_option2);
        q4_moption3 = (RadioButton) findViewById(R.id.q4_option3);
        q4_moption4 = (RadioButton) findViewById(R.id.q4_option4);
        quest5_group = (RadioGroup) findViewById(R.id.q5_options);
        quest5_group.setOnCheckedChangeListener(this);
        q5_mquestion = (TextView) findViewById(R.id.question5_text);
        q5_moption1 = (RadioButton) findViewById(R.id.q5_option1);
        q5_moption2 = (RadioButton) findViewById(R.id.q5_option2);
        q5_moption3 = (RadioButton) findViewById(R.id.q5_option3);
        q5_moption4 = (RadioButton) findViewById(R.id.q5_option4);
        //manswer = (TextView) findViewById(R.id.final_score);
                
                
        dbHelper = new QuizDbAdapter(this);
        dbHelper.open();
        //cursor=dbHelper.fetchAllquiz();//creating a cursor to get results from database
        cursor=dbHelper.fetchquiz_catogery("sports");//creating a cursor to get results from database
        startManagingCursor(cursor);
        //StringBuilder builder = new StringBuilder(); //we create a builder to aggregate a result
        next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//SavePreferences("count",0);
				  // LoadPreferences();
				//manswer.setText(count);
				String str = Integer.toString(count);
				Intent intent = new Intent(mySports.this, result.class);
				intent.putExtra("result", str);
				startActivity(intent);
				/*Intent i=new Intent();
					i.setClass(this, NextMenu.class);
					this.startActivity(i);
					//startActivity(new Intent("com.example.businesscalender.NEXTMENU"));
				 * */
				//manswer.setText(str);
				
				//startActivity(new Intent("com.example.businesscalender.NEXTMENU"));
								
			}
		});
        int i=0;
        while(cursor.moveToNext())
        {
        	String quest = cursor.getString(2);
        	v1.add(quest);
        	//q1_mquestion.setText(quest);
        	String opt1 = cursor.getString(3);
        	v2.add(opt1);
        	//q1_moption1.setText(opt1);
        	String opt2 = cursor.getString(4);
        	v3.add(opt2);
        	//q1_moption2.setText(opt2);
        	String opt3 = cursor.getString(5);
        	v4.add(opt3);
        	//q1_moption3.setText(opt3);
        	String opt4 = cursor.getString(6);
        	v5.add(opt4);
        	//q1_moption4.setText(opt4);
        	
        }
        
        
        dbHelper.close();
        q1_mquestion.setText(v1.elementAt(0));
        q1_moption1.setText(v2.elementAt(0));
        q1_moption2.setText(v3.elementAt(0));
        q1_moption3.setText(v4.elementAt(0));
        q1_moption4.setText(v5.elementAt(0));
        q2_mquestion.setText(v1.elementAt(1));
        q2_moption1.setText(v2.elementAt(1));
        q2_moption2.setText(v3.elementAt(1));
        q2_moption3.setText(v4.elementAt(1));
        q2_moption4.setText(v5.elementAt(1));
        q3_mquestion.setText(v1.elementAt(2));
        q3_moption1.setText(v2.elementAt(2));
        q3_moption2.setText(v3.elementAt(2));
        q3_moption3.setText(v4.elementAt(2));
        q3_moption4.setText(v5.elementAt(2));
        q4_mquestion.setText(v1.elementAt(3));
        q4_moption1.setText(v2.elementAt(3));
        q4_moption2.setText(v3.elementAt(3));
        q4_moption3.setText(v4.elementAt(3));
        q4_moption4.setText(v5.elementAt(3));
        q5_mquestion.setText(v1.elementAt(4));
        q5_moption1.setText(v2.elementAt(4));
        q5_moption2.setText(v3.elementAt(4));
        q5_moption3.setText(v4.elementAt(4));
        q5_moption4.setText(v5.elementAt(4));
       // Toast.makeText(this, q1_moption1.getText(), Toast.LENGTH_LONG);
               
       
		
	}
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch (checkedId){
		case R.id.q1_option1:
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			Toast.makeText(this, q1_moption1.getText().toString(), Toast.LENGTH_SHORT).show();
			cursor=dbHelper.fetchquiz_answer(q1_moption1.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        	//Toast.makeText(this, count, Toast.LENGTH_SHORT).show();
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q1_option2:
			Toast.makeText(this, q1_moption2.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q1_moption2.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q1_option3:
			Toast.makeText(this, q1_moption3.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q1_moption3.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q1_option4:
			Toast.makeText(this, q1_moption4.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q1_moption4.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q2_option1:
			Toast.makeText(this, q2_moption1.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q2_moption1.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q2_option2:
			Toast.makeText(this, q2_moption2.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q2_moption2.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q2_option3:
			Toast.makeText(this, q2_moption3.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q2_moption3.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q2_option4:
			Toast.makeText(this, q2_moption4.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q2_moption4.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q3_option1:
			Toast.makeText(this, q3_moption1.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q3_moption1.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q3_option2:
			Toast.makeText(this, q3_moption2.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q3_moption2.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q3_option3:
			Toast.makeText(this, q3_moption3.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q3_moption3.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q3_option4:
			Toast.makeText(this, q3_moption4.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q3_moption4.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q4_option1:
			Toast.makeText(this, q4_moption1.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q4_moption1.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q4_option2:
			Toast.makeText(this, q4_moption2.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q4_moption2.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q4_option3:
			Toast.makeText(this, q4_moption3.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q4_moption3.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q4_option4:
			Toast.makeText(this, q4_moption4.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q4_moption4.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q5_option1:
			Toast.makeText(this, q5_moption1.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q5_moption1.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q5_option2:
			Toast.makeText(this, q5_moption2.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q5_moption2.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q5_option3:
			Toast.makeText(this, q5_moption3.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q5_moption3.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		case R.id.q5_option4:
			Toast.makeText(this, q5_moption4.getText().toString(), Toast.LENGTH_SHORT).show();
			dbHelper = new QuizDbAdapter(this);
	        dbHelper.open();
			cursor=dbHelper.fetchquiz_answer(q5_moption4.getText().toString());//creating a cursor to get results from database
	        startManagingCursor(cursor);
	        if(cursor.getCount()!=0)
	        {
	        	if(count <5)
	        	{
	        		count = count + 1;
	        	}
	        }
	        else if(count >= 1)
	        {
	        	count = count - 1;
	        }
	        dbHelper.close();
			break;
		}
		
		
	}
	
	
	/*private void SavePreferences(String key, String value){
	    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
	    SharedPreferences.Editor editor = sharedPreferences.edit();
	    editor.putString(key, value);
	    editor.commit();
	   }
	  
	   private void LoadPreferences(){
	    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
	    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
	    String strSavedMem2 = sharedPreferences.getString("MEM2", "");
	   // textSavedMem1.setText(strSavedMem1);
	    //textSavedMem2.setText(strSavedMem2);
	   }*/
	
	

	
	

}
